package wnhz.com.business.shareshop.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.MyCollectionShareBean;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 *共享商城交易记录
 */
public class JiaoYiRecordActivityS extends BaseActivity implements ActionBarClickListener, BaseRecyclerAdapter.OnDataLoadFinish,OnRefreshLoadmoreListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.empty_layout)
    LinearLayout empty;
    @BindView(R.id.tv_msg)
    TextView tv_msg;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int paging = 0;//当前界面
    private BaseRVAdapter mAdapter;
    private List<MyCollectionShareBean.InfoBean> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collects);
        ButterKnife.bind(this);

        actionbar.setData("交易记录", R.drawable.ic_lefft_white, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        setRefrash();
    }
    private void setRefrash() {
        refreshLayout.setOnRefreshLoadmoreListener(this);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        getData();
    }
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = paging + 1;
                getData();
            }
        },2000);
    }
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                paging = 0;
                getData();
            }
        },2000);
    }
    private void getData() {
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getInfoBean().getToken());
        map.put("page", paging+"");
        XUtil.Post(Url.Ucenter_RECORD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.getString("re");
                    if ("1".equals(re)) {
                        Gson gson = new Gson();
                        MyCollectionShareBean item = gson.fromJson(result, MyCollectionShareBean.class);
                        if (paging == 0){
                            if (null != data){
                                data.clear();
                            }
                            data = item.getInfo();
                            refreshLayout.finishRefresh();
                            refreshLayout.resetNoMoreData();
                        }else {
                            refreshLayout.finishLoadmore();
                            mAdapter.notifyDataSetChanged();
                        }
                        setData();
                    }else if ("-1".equals(jsonObject.optString("re"))){
                        MyToast(getResources().getString(R.string.login_out));
                        MyApplication.getInstance().gotoLoginActivity();
                    }else {
                        if (paging == 0){
                            refreshLayout.setVisibility(View.GONE);
                            empty.setVisibility(View.VISIBLE);
                            MyToast(jsonObject.optString("info"));
                            tv_msg.setText("暂无商品");
                        }else {
                            refreshLayout.finishLoadmore();
                            refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                            empty.setVisibility(View.GONE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFinished() {
                super.onFinished();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
    private void setData(){
        recycler.setLayoutManager(new LinearLayoutManager(JiaoYiRecordActivityS.this,LinearLayoutManager.VERTICAL,false));
        mAdapter = new BaseRVAdapter(JiaoYiRecordActivityS.this,data) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_jiaoyirecord;
            }
            @Override
            public void onBind(final BaseViewHolder holder, final int position) {
//                holder.setTextView(R.id.tv_title, data.get(position).getGoods_title());
//                holder.setTextView(R.id.tv_color, data.get(position).getGoods_name());
//                holder.setTextView(R.id.tv_price, "押金¥"+data.get(position).getGroup_price());
//                holder.setTextView(R.id.tv_time, "收藏时间"+data.get(position).getCollect_time());
            }
        };
        recycler.setAdapter(mAdapter);
    }

    @Override
    public void onLeftClick() {
            finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void loadDataFinished(int datasLength) {

    }
}
